﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using CMISLibrary;
using ObjectDanhMuc.DB;


namespace ObjectDanhMuc
{
    public class cls_NPC_MENU_Controller
    {

        #region   Atributes

	    //private DataSet CMIS_Header = new CMISOutputParameter();
	    private DataSet ds = new DataSet();
	    private DataTable dt = new DataTable();
	    private NPC_MENU info;

	    #endregion

        #region   Constructor

	    public cls_NPC_MENU_Controller()
	    {
		    info = new NPC_MENU();
	    }
        public cls_NPC_MENU_Controller(NPC_MENU Info)
	    {
		    this.info = Info;
	    }

	    #endregion
        
        #region   Properties

        public NPC_MENU pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Method

        public IEnumerable<NPC_MENU> select_NPC_MENU_All()
        {
            try
            {
                var q = from p in CMIS2.DB.NPC_MENU
                        orderby p.PARENTMENUID, p.ORDERNUMBER ascending
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_MENU> select_NPC_MENU_by_ID(long longMENUID)
        {
            try
            {
                var q = from p in CMIS2.DB.NPC_MENU
                        orderby p.MENUID ascending
                        where p.MENUID == longMENUID
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_MENU> select_NPC_MENU_by_ArrMENUID(Int64[] ArrMENUID)
        {
            try
            {
                var q = from p in CMIS2.DB.NPC_MENU
                        orderby p.MENUID ascending
                        where ArrMENUID.Contains(p.MENUID)
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_MENU> select_NPC_MENU_by_PARENTID(long longPARENTID)
        {
            try
            {
                var q = from p in CMIS2.DB.NPC_MENU
                        orderby p.MENUID ascending
                        where p.PARENTMENUID == longPARENTID
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public bool insert_NPC_MENU(ref string strErr)
        {
            try
            {
                NPC_MENU objNPC_MENU = new NPC_MENU()
                {
                    SUBDIVISIONID = info.SUBDIVISIONID,
                    LIBID = info.LIBID,
                    MENUID = info.MENUID,
                    HEADER = info.HEADER,
                    PARENTMENUID = info.PARENTMENUID,
                    ORDERNUMBER = info.ORDERNUMBER,
                    CREATEDDATE = info.CREATEDDATE,
                    CREATEDBY = info.CREATEDBY,
                    WORKFLOWID = info.WORKFLOWID,
                    MENU_PARAM = info.MENU_PARAM
                };
                CMIS2.DB.NPC_MENU.InsertOnSubmit(objNPC_MENU);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                CMIS2.ResetDB(); return false;
            }
        }

        public bool update_NPC_MENU()
        {
            try
            {
                NPC_MENU objNPC_MENU = CMIS2.DB.NPC_MENU.Single(c => c.MENUID == info.MENUID);
                objNPC_MENU.SUBDIVISIONID = info.SUBDIVISIONID;
                objNPC_MENU.LIBID = info.LIBID ;
                objNPC_MENU.HEADER = info.HEADER ;
                objNPC_MENU.PARENTMENUID = info.PARENTMENUID ;
                objNPC_MENU.ORDERNUMBER = info.ORDERNUMBER ;
                objNPC_MENU.CREATEDDATE = info.CREATEDDATE ;
                objNPC_MENU.CREATEDBY = info.CREATEDBY ;
                objNPC_MENU.WORKFLOWID = info.WORKFLOWID ;
                objNPC_MENU.MENU_PARAM = info.MENU_PARAM ;

                CMIS2.DB.SubmitChanges();

                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }

        public bool delete_NPC_MENU(ref string strErr)
        {
            try
            {
                var query = from p in CMIS2.DB.NPC_MENU where p.MENUID == info.MENUID select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.NPC_MENU.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        #endregion

        #region SEQ

        public long GetNextSequence(ref string strErr)
        {

            try
            {

                //string strErr = "";
                long _ID = CMIS2.DB.ExecuteCommand("select seq_NPC_MENU.nextval from dual", ref strErr);

                if (strErr == "") return _ID;

                else return -1;

            }

            catch
            {

                CMIS2.ResetDB(); return -1;
            }

        }
        
       

        #endregion 
    }
}
